/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.web;

import cz.muni.fi.pa165.portalnahouby.api.dto.Edibility;
import cz.muni.fi.pa165.portalnahouby.api.dto.MushroomDTO;
import cz.muni.fi.pa165.portalnahouby.api.serviceinterface.MushroomService;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.ValidationErrors;
import org.springframework.orm.jpa.JpaSystemException;

/**
 *
 * @author Jakub Kutil
 */
@UrlBinding("/mushrooms/{$event}/{mushroom.id}")
public class MushroomsActionBean implements ActionBean {

    private ActionBeanContext context;
    private int show;

    public int getShow() {
        return show;
    }

    public void setShow(int show) {
        this.show = show;
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }
    @SpringBean
    private MushroomService service;

    @DefaultHandler
    public Resolution all() {
        return new ForwardResolution("/mushroom.jsp");
    }

    public List<MushroomDTO> getMushrooms() {
        List<MushroomDTO> result = new ArrayList<MushroomDTO>();
        switch (show) {
            default:
            case 1:
                result = service.getListSortByName();
                break;
            case 2:
                result = service.getListByEdibility(Edibility.JEDLA);
                break;
            case 3:
                result = service.getListByEdibility(Edibility.NEJEDLA);
                break;
            case 4:
                result = service.getListByEdibility(Edibility.JEDOVATA);
                break;
        }

        return result;
    }
    @ValidateNestedProperties(value = {
        @Validate(on = {"add", "save"}, field = "name", required = true, maxlength = 255)
    })
    private MushroomDTO mushroom;

    public MushroomDTO getMushroom() {
        return mushroom;
    }

    public void setMushroom(MushroomDTO mushroom) {
        this.mushroom = mushroom;
    }

    
    public Resolution add() {
        try {
            service.addMushroom(mushroom);

        } catch (JpaSystemException jse) {
            ValidationErrors errors = new ValidationErrors();
            errors.add("name", new LocalizableError("mushroom.duplicity"));
            getContext().setValidationErrors(errors);
            return getContext().getSourcePageResolution();
        }
        return new RedirectResolution(this.getClass(), "all");
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadMushroomFromDatabase() {
        String ids = context.getRequest().getParameter("mushroom.id");
        if (ids == null) {
            return;
        }
        mushroom = service.findMushroom(Long.parseLong(ids));
    }

    
    public Resolution edit() {
        return new ForwardResolution("/mushroomedit.jsp");
    }

    
    public Resolution save() {
        try {
            service.editMushroom(mushroom);

        } catch (JpaSystemException jse) {
            ValidationErrors errors = new ValidationErrors();
            errors.add("name", new LocalizableError("mushroom.duplicity"));
            getContext().setValidationErrors(errors);
            return getContext().getSourcePageResolution();
        }
        return new RedirectResolution(this.getClass(), "all");
    }

    
    public Resolution delete() {
        try {
            service.deleteMushroom(mushroom.getId());

        } catch (JpaSystemException jse) {
            ValidationErrors errors = new ValidationErrors();
            errors.add("name", new LocalizableError("mushroom.inForest"));
            getContext().setValidationErrors(errors);
            return getContext().getSourcePageResolution();
        }
        return new RedirectResolution(this.getClass(), "all");
    }

    public Resolution showAll() {
        show = 1;

        return new ForwardResolution(this.getClass(), "all");
    }

    public Resolution showEdible() {
        show = 2;

        return new ForwardResolution(this.getClass(), "all");
    }

    public Resolution showNotEdible() {
        show = 3;

        return new ForwardResolution(this.getClass(), "all");
    }

    public Resolution showPoisonous() {
        show = 4;

        return new ForwardResolution(this.getClass(), "all");
    }
}